package com.spark.data

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.ext.alert
import com.spark.common.ext.sdf
import com.spark.data.model.MaterialQrResultListModel
import kotlinx.android.synthetic.main.frag_borrow_apply.*

/**
 * appy this before borrow data
 */
class BorrowApplyInfoFragment : BaseFragment() {
    private var borrowItem: MaterialQrResultListModel.ResultMap? = null

    companion object {
        fun newInstance(itemStr: String): BorrowApplyInfoFragment {
            val target = BorrowApplyInfoFragment()
            val args = Bundle()
            args.putString("borrowItem", itemStr)
            target.arguments = args
            return target
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_borrow_apply
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        borrowItem = arguments?.getString("borrowItem")?.let { Gson().fromJson(it, MaterialQrResultListModel.ResultMap::class.java) }

        borrowItem?.apply {
            iv_back.setOnClickListener { pop() }
            tv_title.text = "借阅申请"
            //tv_data_name.text = this.name
            tv_data_name.text = "2018安全生产标准试行办法"
            tv_serial.text = "编号: ${this.number}"
            tv_regular.text = "规范: ${this.stdname}"
            tv_create_time.text = "创建: ${sdf().format(this.timestamp)}"
            //tv_tags.text = "标签: ${this.keysName}"
            tv_tags.text = "标签: 安全、标准、试行"
            recy_books.layoutManager = object : LinearLayoutManager(context) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            val queryBookAdapter = QueryBookAdapter(R.layout.item_borrow_book, false)
            recy_books.adapter = queryBookAdapter
            queryBookAdapter.addData(this.fileList)

            tv_create.text = sdf().format(this.timestamp)

            tv_chk_approve.setOnClickListener {
                val reason = et_reason.text.toString().trim()
                if (reason.isNullOrBlank())
                    alert("请填写您的借阅理由")
                else {
                }
            }
        }

    }
}